<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>

<style>
	#inputTable {text-align:center;border:1px solid #ddd; border-width:0 0 1px 1px;}
	
	#inputTable tr,#inputTable td {height:22px; border:1px solid #ddd; border-width:1px 1px 0 0;}
</style>

<script type="text/javascript">
<!--
	function onSave(){
	 	var errorMsg = "";
		if($("#clientId").val().trim()=="" || $("#gatwayId").val().trim()==""){
			errorMsg = '<s:text name="dialog.message.tip.needRequiredFields"/>' ;
		}
		
		if($("#mawbNoFirst").val().trim()!="" && $("#mawbNoFirst").val().trim().length!=3){
			errorMsg = '<s:text name="dialog.message.tip.mawbFormatteWrong"/>' ;
		}
		if($("#mawbNoSeconde").val().trim()!="" && $("#mawbNoSeconde").val().trim().length!=8){
			errorMsg = '<s:text name="dialog.message.tip.mawbFormatteWrong"/>' ;
		}
		
		if($("#stateChangeTime").is(":hidden") == false && $("#stateChangeTime").val().trim() == ""){ 
			errorMsg = '<s:text name="dialog.message.tip.needRequiredFields"/>' ;
		} 
		
		var mawbValue = $("#mawbNoSeconde").val().trim();
		if($("#mawbNoSeconde").val().trim()!=""){
			 var caculatedValue =  (parseInt("1" + mawbValue.substring(0,7)) - 10000000) % 7;
		     if(caculatedValue != parseInt(mawbValue.substring(7,8))){
		    	 errorMsg = '<s:text name="dialog.message.tip.mawbCheckNumberWrong"/>' ;
		     };
		}
		
		if(errorMsg != ""){
			showErrorTip(errorMsg);
			return false;
		}
		return true;
	}
	
	var mawbShipperTips=["1","2"];
	var mawbConsigneeTips=[""];
	var hawbShipperTips=[""];
	var hawbConsigneeTips=[""];
	
	function selectClient(){
		 $("#clientName").val("");
		 var clientId = $("#clientId").val();
		 if(clientId != ""){
			 $.getJSON("Json_getClientName.do?userId="+$("#clientId").val(),function(result){
				 $("#clientName").val(result.clientName);
		     });
			 
			 $.getJSON("Json_getUserTips.do?userId="+$("#clientId").val()+"&random="+Math.random(),function(result){
				 var mawbShipperTips=new Array();
				 var mawbConsigneeTips=new Array();
				 var hawbShipperTips=new Array();
				 var hawbConsigneeTips=new Array();
				 for(var i=0;i<result.length;i++){
					 if(result[i].tipType == 1){
						 mawbShipperTips.push(result[i].tipValue);
					 }
					 if(result[i].tipType == 2){
						 mawbConsigneeTips.push(result[i].tipValue);
					 }
					 if(result[i].tipType == 3){
						 hawbShipperTips.push(result[i].tipValue);
					 }
					 if(result[i].tipType == 4){
						 hawbConsigneeTips.push(result[i].tipValue);
					 }
				 }
				 $( "#mawbShipper" ).autocomplete({
						source: mawbShipperTips
				 });
				 $( "#mawbConsignee" ).autocomplete({
						source: mawbConsigneeTips
				 });
				 $( "#hawbShipper" ).autocomplete({
						source:  hawbShipperTips
				 });
				 $( "#hawbConsignee" ).autocomplete({
						source: hawbConsigneeTips
				 });
		     });
		 }
	}
	
	function freezeSA(){
		var isFreeze = $("#isFreeze").val();
		if(isFreeze == "true"){
			$("#inputTable input,select,textarea").each(function (){
		         $(this).attr("disabled","disabled");
			});
			$("#btn_reset").attr("disabled","disabled");
		}
		$("#btn_reset").attr("disabled","");
		$("#btn_comment").attr("disabled","");
		$("#btn_attachment").attr("disabled","");
		$("#stateChangeTime").attr("disabled","");
		$("#btn_save").attr("disabled","");
		$("#btn_reset").attr("disabled","");
		$("#state").attr("disabled","");
		$("#newRemark").attr("disabled","");
		
		$("#remarksOnHawb").attr("disabled","");
		$("#remark").attr("disabled","");
	}
	
	function viewComments(saId){
		window.location.href = "./SACommentMgr_search.do?saId="+saId+"&saType=air";
	}
	
	function demensionInput(){
		var estimatedDimensionsValue = $("#estimatedDimensions").val().trim();
		if(estimatedDimensionsValue != ""){
			$("#vweight").val((parseFloat(estimatedDimensionsValue) / 0.006) + "");
		}else{
			$("#vweight").val("");
		}
	}
	
	function viewAttachments(saId){
		window.location.href = "./AttachmentMgr_index.do?attachment.saId="+saId;
	}
	
	function changeState(){
		var olderState = $("#olderState").val();
		var nowState = $("#state").val();
		
		if(olderState+"" != nowState+""){
			$("#changeTimeLable").show("fast");
			$("#changeTimeValue").show("fast");
		}else{
			$("#changeTimeLable").hide("fast");
			$("#changeTimeValue").hide("fast");
		}
	}
	
	function showRemarkHistory(){
		if($("#remarkHistory").is(":hidden")){
			$("#remarkHistory").show("slow");
			$("#a4remarkShow").html("Hiden Remark History");
		}else{
			$("#remarkHistory").hide("slow");
			$("#a4remarkShow").html("Show Remark History");
		}
	}
	
	function disableSa(saId){
	    var offset = $("#offset").val();
		window.location.href = "./SaMgr_updateSaStatus.do?updateStatus=0&editSaId="+saId;
	}
	
	function enableSa(saId){
		var offset = $("#offset").val();
		window.location.href = "./SaMgr_updateSaStatus.do?updateStatus=1&editSaId="+saId;
	}
	
	function viewSop(saId,clientId){
		window.location.href = "./UserMgr_viewSop.do?saId="+saId+"&clientId="+clientId;
	}
	
	function addDlvStation(saId){
		var feature = "dialogWidth:700px;dialogHeight:400px;center:yes;status:no;help:no;dialogLeft:250px;dialogTop:300";
	    showModalDialog("./SaMgr_addSAStateHisIndex.do?editSaId="+saId,window,feature);
	}
	
	$(document).ready(function (){
		selectClient();
		freezeSA();
		showRemarkHistory();
	}); 
	
//-->
</script>

<s:include value="./component/tip_box.jsp"></s:include>

<s:form action="SaMgr_saveAirSa" validate="false" onsubmit="return onSave()">
	<s:hidden key="airSa.id"/>
	<s:hidden key="airSa.saNo"/>
	<s:hidden key="airSa.status"/>
	<s:hidden id="olderState" key="airSa.saOlderState"/>
	<s:hidden key="airSa.createBy_id"/>
	<s:hidden key="airSa.flagRead"/>
	
	<c:if test="${isFreeze == 'true'}">
		<s:hidden key="airSa.clientId"/>
		<s:hidden key="airSa.origin"/>
		<s:hidden key="airSa.destination"/>
		<s:hidden key="airSa.gatwayId"/>
		<s:hidden key="airSa.weight"/>
		<s:hidden key="airSa.pcb"/>
		<s:hidden key="airSa.estimatedDimensions"/>
		<s:hidden key="airSa.goodsNatural"/>
		<s:hidden key="airSa.product"/>
		<s:hidden key="airSa.orderNo"/>
		<s:hidden key="airSa.flightDate">
			 <s:param name="value" ><s:date name="airSa.flightDate" format="yyyy-MM-dd" /></s:param> 
		</s:hidden>
		<s:hidden key="airSa.estimatedTimeArrival">
			 <s:param name="value" ><s:date name="airSa.estimatedTimeArrival" format="yyyy-MM-dd" /></s:param> 
		</s:hidden>
		<s:hidden key="airSa.consolidationAllowed"/>
		<s:hidden key="airSa.hawbPaymentOptions"/>
		<s:hidden key="airSa.rate"/>
		<s:hidden key="airSa.volumeCutAllowed"/>
		<s:hidden key="airSa.flyUsaAllowed"/>
		<s:hidden key="airSa.flyNrtAllowed"/>
		<s:hidden key="airSa.hawbNo"/>
		<s:hidden key="airSa.mawbNo"/>
		<s:hidden key="airSa.mawbShipper"/>
		<s:hidden key="airSa.mawbConsignee"/>
		<s:hidden key="airSa.hawbShipper"/>
		<s:hidden key="airSa.hawbConsignee"/>
		<s:hidden key="airSa.flightNo"/>
		<s:hidden key="airSa.vweight"/>
		<s:hidden key="airSa.dlvPlace"/>
		<s:hidden key="airSa.term"/>
		<s:hidden key="airSa.isAuthorization"/>
	</c:if>
	<input type="hidden" id="isFreeze" value="${isFreeze}"/>
<div style="background-color:#F5F5F5;margin-top:1cm;">
	<div align="left">
		<s:actionmessage cssStyle="color:#0000FF;"/>
		<s:actionerror cssStyle="color:#F00;"/>
	</div>
	<table id="inputTable" width="100%" >
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.saNo"/>:
			</td>
			<td align="left">
				<s:label name="airSa.saNo"></s:label>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.gatwayId"/><font color="red">*</font>:
			</td>
			<td align="left">
					<c:if test="${airSa.id!=null}">
				 		 <s:select id="gatwayId" name="airSa.gatwayId" list="usableGatways" disabled="true"/>
				 		 <s:hidden key="airSa.gatwayId" />
					</c:if>
					<c:if test="${airSa.id==null}">
						<s:select id="gatwayId" name="airSa.gatwayId" list="usableGatways"/>
					</c:if>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.state"/><font color="red">*</font>:
			</td>
			<td align="left">
			    <s:select id="state" name="airSa.state" list="usableStates" onchange="changeState()"/>
			</td>
			<c:if test="${airSa.id!=null}">
				<td id="changeTimeLable" align="right" style="display: none;background:#ececec;" class="label_bold">
					<s:text name="sa.stateChangeTime"/><font color="red">*</font>:
				</td>
				<td id="changeTimeValue" align="left"  style="display: none;">
					<s:textfield id="stateChangeTime" name="airSa.stateChangeTime" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',readOnly:true,lang:'en'})"/>
				</td>
			</c:if>
			<c:if test="${airSa.id==null}">
				<td id="changeTimeLable" align="right" style="background:#ececec;" class="label_bold">
					<s:text name="sa.stateChangeTime"/><font color="red">*</font>:
				</td>
				<td id="changeTimeValue" align="left" >
					<s:textfield id="stateChangeTime" name="airSa.stateChangeTime" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',readOnly:true,lang:'en'})"/>
				</td>
			</c:if>
		</tr>
	
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.clientID"/><font color="red">*</font>:
			</td>
			<td align="left">
				<c:if test="${airSa.id==null}">
			  		 <s:select id="clientId" name="airSa.clientId" list="customers" onchange="selectClient()"/>
				</c:if>
				<c:if test="${airSa.id!=null}">
					 <s:select id="clientId" name="airSa.clientId" list="customers" onchange="selectClient()" disabled="true"/>
					 <s:hidden key="airSa.clientId"/>
				</c:if>
			</td>
			
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.clientName"/><font color="red">*</font>:
			</td>
			<td align="left">
				<input type="text" id="clientName" disabled="disabled">
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.origin"/>:
			</td>
			<td align="left">
				<s:textfield id="origin" name="airSa.origin"/>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.destination"/>:
			</td>
			<td align="left">
				<s:textfield id="destination" name="airSa.destination"/>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.pcb"/>:
			</td>
			<td align="left">
				<s:if test="airSa.pcb != null">
					<s:textfield id="pcb" name="airSa.pcb" onkeyup="clearNoNum(this)" value="%{getText('format.sa.pcb',{airSa.pcb})}" />
				</s:if>
				<s:else>
					<s:textfield id="pcb" name="airSa.pcb" onkeyup="clearNoNum(this)"/>
				</s:else>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.estimatedDimensions"/>:
			</td>
			<td align="left">
				<s:if test="airSa.estimatedDimensions != null">
					<s:textfield id="estimatedDimensions" name="airSa.estimatedDimensions" value="%{getText('format.sa.estimatedDimensions',{airSa.estimatedDimensions})}" onkeyup="clearNoNum(this),demensionInput()"/>CBM
				</s:if>
				<s:else>
					<s:textfield id="estimatedDimensions" name="airSa.estimatedDimensions"  onkeyup="clearNoNum(this),demensionInput()"/>CBM
				</s:else>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				 <s:text name="sa.weight"/>:
			</td>
			<td align="left">
				<s:if test="airSa.weight != null">
					<s:textfield id="weight" name="airSa.weight" value="%{getText('format.sa.weight',{airSa.weight})}" onkeyup="clearNoNum(this)"/>KGS
				</s:if>
				<s:else>
					<s:textfield id="weight" name="airSa.weight" onkeyup="clearNoNum(this)"/>KGS
				</s:else>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				 <s:text name="sa.vweight"/>:
			</td>
			<td align="left" >
				<s:if test="airSa.vweight != null">
					<s:textfield id="vweight" name="airSa.vweight" value="%{getText('format.sa.weight',{airSa.vweight})}" onkeyup="clearNoNum(this)"/>
				</s:if>
				<s:else>
					<s:textfield id="vweight" name="airSa.vweight" onkeyup="clearNoNum(this)"/>KGS
				</s:else>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.goodsNatural"/>:
			</td>
			<td align="left">
				<s:textfield id="goodsNatural" name="airSa.goodsNatural"/>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.orderNo"/>:
			</td>
			<td align="left">
				<s:textfield id="orderNo" name="airSa.orderNo"/>
			</td>
		</tr>
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="label.sa.flightno"/>:
			</td>
			<td align="left">
				<s:textfield id="flightNO" name="airSa.flightNo"/>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.flightDate"/>:
			</td>
			<td align="left">
				 <s:textfield id="flightDate" name="airSa.flightDate"  onfocus="WdatePicker({readOnly:true,lang:'en'})"> 
			        <s:param name="value" ><s:date name="airSa.flightDate" format="yyyy-MM-dd" /></s:param> 
			    </s:textfield>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.estimatedTimeArrival"/>:
			</td>
			<td align="left">
				<s:textfield id="estimatedTimeArrival" name="airSa.estimatedTimeArrival" onfocus="WdatePicker({readOnly:true,lang:'en'})">
					<s:param name="value" ><s:date name="airSa.estimatedTimeArrival" format="yyyy-MM-dd" /></s:param> 
				</s:textfield>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
			</td>
			<td align="left">
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.consolidationAllowed"/>:
			</td>
			<td align="left">
				<s:textfield id="consolidationAllowed" name="airSa.consolidationAllowed"/>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.hawbPaymentOptions"/>:
			</td>
			<td align="left">
				<s:textfield id="hawbPaymentOptions" name="airSa.hawbPaymentOptions"/>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.rate"/>:
			</td>
			<td align="left">
				<s:if test="airSa.rate != null">
				   <s:textfield id="rate" name="airSa.rate" onkeyup="clearNoNum(this)"  value="%{getText('format.sa.rate',{airSa.rate})}"/>
				</s:if>
				<s:else>
				   <s:textfield id="rate" name="airSa.rate" onkeyup="clearNoNum(this)" />
				</s:else>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.volumeCutAllowed"/>:
			</td>
			<td align="left">
				<s:textfield id="volumeCutAllowed" name="airSa.volumeCutAllowed"/>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   AUTHORIZATION:
			</td>
			<td align="left">
				<s:select name="airSa.isAuthorization" list="authorizationOption"></s:select>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				TERM:
			</td>
			<td align="left">
				<s:select name="airSa.term" list="termOption"></s:select>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.flyUsaAllowed"/>:
			</td>
			<td align="left">
				<s:select name="airSa.flyUsaAllowed" list="flyAllowOption"></s:select>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.flyNrtAllowed"/>:
			</td>
			<td align="left">
				<s:select name="airSa.flyNrtAllowed" list="flyAllowOption"></s:select>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.mawbNo"/>:
			</td>
			<td align="left">
				<s:textfield id="mawbNoFirst" name="airSa.mawbNoFirst" size="3" maxlength="3"/>-<s:textfield id="mawbNoSeconde" name="airSa.mawbNoSeconde" maxlength="8" size="8" onkeyup="clearNoNum(this)"/>
			</td>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.hawbNo"/>:
			</td>
			<td align="left">
				<s:textfield id="hawbNoFirst" name="airSa.hawbNoFirst" size="3" maxlength="3" readonly="true"/>-<s:textfield id="hawbNoSeconde" name="airSa.hawbNoSeconde" maxlength="7" size="7" readonly="true"/>
			</td>
		</tr>
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.mawbShipper"/>:
			   <input type="checkbox" name="member_mawbShipper" value="1" title="remeber this input as Client Tip!">
			</td>
			<td align="left" colspan="3">
				<s:textarea id="mawbShipper" cols="25" rows="3" cssStyle="width: 580px;font-family: Arial;" name="airSa.mawbShipper"></s:textarea>
			</td>
		</tr>
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.mawbConsignee"/>:
			   <input type="checkbox" name="member_mawbConsignee" value="1" title="remeber this input as Client Tip!">
			</td>
			<td align="left" colspan="3" >
				<s:textarea id="mawbConsignee" cols="25" rows="3" cssStyle="width: 580px;font-family: Arial;" name="airSa.mawbConsignee"></s:textarea>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.hawbShipper"/>:
				  <input type="checkbox" name="member_hawbShipper" value="1" title="remeber this input as Client Tip!">
			</td>
			<td align="left" colspan="3"  >
				<s:textarea id="hawbShipper" cols="25" rows="3" cssStyle="width: 580px;font-family: Arial;"  name="airSa.hawbShipper"></s:textarea>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
				<s:text name="sa.hawbConsignee"/>:
				<input type="checkbox" name="member_hawbConsignee" value="1" title="remeber this input as Client Tip!">
			</td>
			<td align="left" colspan="3">
				<s:textarea id="hawbConsignee" cols="25" rows="3" cssStyle="width: 580px;font-family: Arial;"  name="airSa.hawbConsignee"></s:textarea>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.remarksOnHawb"/>:
			</td>
			<td colspan="3" align="left">
				<s:textfield id="remarksOnHawb" name="airSa.remarksOnHawb" cssStyle="width: 580px"/>
			</td>
		</tr>
		
		<tr>
			<td align="right" style="background:#ececec;" class="label_bold">
			   <s:text name="sa.remark"/>:
			</td>
			<td colspan="3" align="left">
				<s:textfield id="newRemark" name="airSa.newRemark" cssStyle="width: 580px"/>
				<a id="a4remarkShow" href="javascript:void(0)" onclick="showRemarkHistory(this)">Show Remark History</a>
			</td>
		</tr>
		<tr id="remarkHistory" style="display: none;">
			<td align="right" style="background:#ececec;" class="label_bold">
				Remark History:
			</td>
			<td align="left" colspan="3">
				<s:textarea id="remark" cols="25" rows="5" cssStyle="width: 580px;font-family: Arial;"  name="airSa.remark" readonly="true"></s:textarea>
			</td>
		</tr>
		
		
		<c:if test="${airSa.id!=null}">
			<tr>
				<s:include value="./component/air_sa_hises.jsp">
				</s:include>
			</tr>
		</c:if>
		<tr>
			<td colspan="4" align="center">
				<s:submit id="btn_save" key="action.save" action="SaMgr_saveAirSa" cssClass="btn"/>
				<c:if test="${airSa.id!=null}">
					<input id="btn_comment" class="btn" type="button" value="<s:text name="action.comments"/>" onclick="viewComments('${airSa.id}')">
					<input id="btn_attachment" class="btn" type="button" value="<s:text name="action.attachments"/>" onclick="viewAttachments('${airSa.id}')">
					<input id="btn_sop" class="btn" type="button" value="SOP" onclick="viewSop(${airSa.id},${airSa.client.id})">
					<c:if test="${airSa.status == 1}">
						<input type="button" class="btn" value="<s:text name="entity.status.Disabled"/>" onclick="disableSa('${airSa.id}')">
					</c:if>
					<c:if test="${airSa.status == 0}">
						<input type="button" class="btn" value="<s:text name="entity.status.Enabled"/>" onclick="enableSa('${airSa.id}')">
					</c:if>
					<c:if test="${sessionScope.user.isRoot == true}">
						<c:if test="${airSa.state == 71}">
							<input type="button" class="btn" value="Add TRT" onclick="addDlvStation('${airSa.id}')">
						</c:if>
					</c:if>
				</c:if>
        		<s:reset id="btn_reset" key="action.reset" cssClass="btn"/>
			</td>
		</tr>
	</table>
</div>	
</s:form>